package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.activity.find.StrategyDetailActivity;
import com.mz.ozy.adapter.CardBookListAdapter;
import com.mz.ozy.base.BaseFragment;
import com.mz.ozy.bean.MyBookBean;
import com.mz.ozy.bean.MyShopBean;
import com.mz.ozy.mvp.presenter.MyKeepPresenter;
import com.mz.ozy.mvp.view.MyKeepContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author lifeStyle on 2020/4/3 0003.
 */
public class MyCardFragment extends BaseFragment<MyKeepContract.View, MyKeepPresenter> implements MyKeepContract.View {

    public static final int NO = 0;
    public static final int ALREADY = 1;
    public static final int DATA = 2;
    Unbinder unbinder;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private CardBookListAdapter mAdapter;
    private List<MyBookBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();

    private int pageNo = 1;
    private int tag = 0;

    public static MyCardFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        MyCardFragment fragment = new MyCardFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPresenter == null) return;
        tag = 0;
        pageNo = 1;
        mPresenter.getBook(mActivity, pageNo + "");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_recharger_record, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }


    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new CardBookListAdapter(mBeanList);
        recyclerview.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyBookBean.PageBean.PageItemsBean item = (MyBookBean.PageBean.PageItemsBean) adapter.getItem(position);
                Intent intent = new Intent(getActivity(), StrategyDetailActivity.class);
                intent.putExtra(StrategyDetailActivity.ARTICLE_ID, item.getId());
                startActivity(intent);
            }
        });
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getBook(mActivity, pageNo + "");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getBook(mActivity, pageNo + "");
            }
        });
    }

    @Override
    public void getBookSuccess(MyBookBean bean) {
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void getCanclSuccess(JSONObject jsonObject) {

    }

    @Override
    public void getShopSuccess(MyShopBean bean) {

    }
}
